package com.jframe.sso.action;

import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import com.jframe.sso.model.TotleVisitNum;
import com.jframe.sso.model.VisitCount;
import com.jframe.sso.service.TotleVisitNumService;
import com.jframe.sso.service.VisitCountService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.SpringUtil;

public class VisitCountAction extends ExtendAction {
	private static String ACTION="visitCountAction";
	private static String SERVICE="visitCountService";
	private static Class<VisitCount> CLAZZ=VisitCount.class;
	
	public void countVisitNumTask(Object[] args){
		this.log.debug("用户访问统计开始！");
		VisitCountService visitCountService = (VisitCountService)this.getBean("visitCountService");
		try{
			ServletContextEvent args0 = (ServletContextEvent) args[0];
			ServletContext application = args0.getServletContext();
			int anonyCount = (Integer) application.getAttribute("anonyUserCount");
			int registerCount = (Integer) application.getAttribute("registerUserCount");
			VisitCount visitCount = visitCountService.getVisitCountByDate(CalendarUtil.getDate()+"", CalendarUtil.getHour()+"");
			visitCount.setAnonyCount(anonyCount);
			visitCount.setRegisterCount(registerCount);
			visitCount.setTotleNum(anonyCount+registerCount);
			visitCount.setDate(CalendarUtil.getDate());
			visitCount.setHour(CalendarUtil.getHour()+"");
			visitCountService.save(visitCount);
			
			TotleVisitNumService totleVisitNumService = (TotleVisitNumService)SpringUtil.getBean("totleVisitNumService");
			TotleVisitNum totleVisitNum = totleVisitNumService.load(1);
			int allVisitNum = (Integer) application.getAttribute("systemTotleVisitNum_");
			if(totleVisitNum.getTotleVisitNum()<allVisitNum){
				totleVisitNum.setTotleVisitNum(allVisitNum);
				totleVisitNumService.save(totleVisitNum);
			}
		}catch(Exception e){
			//e.printStackTrace();
		}
		this.log.debug("用户统计完成！");
	}
}
